#include "CAnimatedObject.h"


CAnimatedObject::CAnimatedObject():CObject()
{
	m_XVelocity		= 0.0f;
	m_YVelocity		= 0.0f;
	m_fGravity		= 800.0f;
	m_pSprite		= NULL;
	m_fOffsetX		= 0;
	m_fOffsetY		= 0;
	m_nOffsetWidth	= 0;
	m_nOffsetHeight = 0;
}

CAnimatedObject::CAnimatedObject(float Xpos, float Ypos, float deep , float XVelocity, float YVelocity, int width, int height):CObject(Xpos , Ypos , deep , width , height)
{
	m_XVelocity		= XVelocity;
	m_YVelocity		= YVelocity;
	m_fGravity		= 800.0f;
	m_pSprite		= NULL;
	m_fOffsetX		= 0;
	m_fOffsetY		= 0;
	m_nOffsetWidth	= width;
	m_nOffsetHeight = height;
}

CAnimatedObject::~CAnimatedObject()
{
	if(m_pSprite)
		m_pSprite->Release();
	m_pSprite = NULL;
}



//--------------------------------------------------//
// Name : LoadSprite								//
// Desc : Loading a sprite for this animated object //
//--------------------------------------------------//
void CAnimatedObject::LoadSprite(CTexture* image , int NumFrame , int NumFrameInRow )
{
	SetImage(image);
	m_pSprite = new CSprite(NumFrame , NumFrameInRow , GetWidth() , GetHeight() );
}



//------------------------------------//
// Name : Update					  //
// Desc : Update this animated object //
//------------------------------------//
void CAnimatedObject::Update(float deltaTime)
{
	m_pSprite->Update( deltaTime );
}


//------------------------------------------------//
// Name : Render								  //
// Desc : Render this animated object into screen //
//------------------------------------------------//
void CAnimatedObject::Render( float camPosX /* = 0*/ , float camPosY /*= 0*/ )
{
	m_pImage->Draw(GetXposition() - camPosX , GetYposition() - camPosY , 
			m_pSprite->GetVecScaleCenter() , m_pSprite->GetXScaleRatio() , m_pSprite->GetYScaleRatio() , 
			m_pSprite->GetVecRotateCenter() , m_pSprite->GetAngle() , 
			m_pSprite->GetColor() , 
			&m_pSprite->GetSourceRect() , GetDeep() );
}



//-----------------------------------------//
// Name : SetVelocity                      //
// Desc : Set new velocity for this object //
//-----------------------------------------//
void CAnimatedObject::SetVelocity(float NewXVelocity , float NewYVelocity)
{
	m_XVelocity = NewXVelocity;
	m_YVelocity = NewYVelocity;
}


//-------------------------------------------//
// Name : SetXvelocity                       //
// Desc : Set new X velocity for this object //
//-------------------------------------------//
void CAnimatedObject::SetXvelocity(float NewXVelocity )
{
	m_XVelocity = NewXVelocity;
}


//-------------------------------------------//
// Name : SetYvelocity                       //
// Desc : Set new Y velocity for this object //
//-------------------------------------------//
void CAnimatedObject::SetYvelocity(float NewYVelocity )
{
	m_YVelocity = NewYVelocity;
}



//------------------------------------------//
// Name : SetGravity						//
// Desc : Set the gravity of this object	//
//------------------------------------------//
void CAnimatedObject::SetGravity( float NewGravity )
{
	m_fGravity = NewGravity;
}



//--------------------------------------//
// Name : CreateBoundBox				//
// Desc : Creating an new bounding box	//
//--------------------------------------//
void CAnimatedObject::CreateBoundBox(  float offsetX , float offsetY , int width , int height )
{
	if( width <= 0 )
		width = GetWidth();
	if(height <= 0 )
		height = GetHeight();
	m_fOffsetX		= offsetX;
	m_fOffsetY		= offsetY;
	m_nOffsetWidth	= width;
	m_nOffsetHeight	= height;
}



//--------------------------------------------------//
// Name : GetXVelocity                              //
// Desc : Get the current X velocity of this object //
//--------------------------------------------------//
float CAnimatedObject::GetXVelocity( )
{
	return m_XVelocity;
}


//--------------------------------------------------//
// Name : GetYVelocity                              //
// Desc : Get the current Y velocity of this object //
//--------------------------------------------------//
float CAnimatedObject::GetYVelocity( )
{
	return m_YVelocity;
}



//--------------------------------------------------//
// Name : GetGravity								//
// Desc : Get the current gravity of this object	//
//--------------------------------------------------//
float CAnimatedObject::GetGravity()
{
	return m_fGravity;
}


//--------------------------------------------------------------------------//
// Name : GetSprite															//
// Desc : Get the pointer of sprite instance to set or get info of sprite	//
//			like scale , rotate , animation ....							//
//--------------------------------------------------------------------------//
CSprite* CAnimatedObject::GetSprite() const
{
	if(m_pSprite)
		return m_pSprite;

	return NULL;
}



//--------------------------------------------------//
// Name : GetBoundBox								//
// Desc : Get the bound box of this animated object	//
//--------------------------------------------------//
RECT CAnimatedObject::GetBoundBox()
{
	RECT r;
	
	r.left		= GetXposition() + m_fOffsetX;
	r.right		= r.left + m_nOffsetWidth * abs(m_pSprite->GetXScaleRatio());
	r.top		= GetYposition() + m_fOffsetY;
	r.bottom	= r.top + m_nOffsetHeight * abs(m_pSprite->GetYScaleRatio());

	return r;
}



void CAnimatedObject::MoveBoundBox(float offsetX, float offsetY)
{
	m_fOffsetX += offsetX;
	m_fOffsetY += offsetY;
}



